﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernateDB;

namespace MedicalDB
{
    public partial class AdaugarePacient : UserControl
    {
        private Control redControl;
        string limba;
        Dictionary<string, string> namesDANU;
        
        public AdaugarePacient(string limba)
        {
            InitializeComponent();
            Rename(limba);
            this.limba = limba;

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            namesDANU = xmlParser.ExtractAttributePairs("DANU", string.Empty, string.Empty, "name", "formula");

        }

        private void btAdaugarePacient_Click(object sender, EventArgs e)
        {
            if(redControl!=null)
                changeColor(redControl);

            List<Control> toValidate = new List<Control>();
            bool isValid = true;

            DateGeneralePacient dgp = new DateGeneralePacient();
            dgp.IdPacient = tbCNP.Text;
            dgp.Nume = tbPacientNume.Text;
            dgp.Prenume = tbPacientPrenume.Text;
            dgp.Sex = cbSex.Text;
            dgp.DataNasterii = dtDataNasterii.Value;
            dgp.LoculNasterii = tbLoculNasterii.Text;
            dgp.Resedinta = tbResedinta.Text;
            dgp.DomiciliuLegal = tbDomiciliuLegal.Text;
            dgp.GrupaSanguina = tbGrupaSange.Text;
            dgp.Ocupatie = tbOcupatie.Text;
            dgp.LocMunca = tbLocMunca.Text;
            dgp.TalonPensie = tbTalonPensie.Text;

            FiseInternare fi = new FiseInternare();
            fi.IdPacient = tbCNP.Text;
            fi.DiagnosticInternare = tbDiagnostic.Text;
            fi.Diagnostic72h = tbDiagnostic72.Text;
            fi.DiagnosticTrimitere = tbDiagnosticTrimitere.Text;
            fi.DataInternarii = dtDataInternarii.Value;
            fi.MotiveleInternarii = tbMotiveleInternarii.Text;
            fi.DiagnosticPrincipal = tbDiagnosticPrincipal.Text;
            fi.BoliConcomitente = tbBoliConcomitente.Text;

            if (cbInternatUrgenta.Checked)
                fi.InternatUrgenta = namesDANU["DA"];
            else
                fi.InternatUrgenta = namesDANU["NU"];

            fi.Sectia = tbSectie.Text;
            fi.Salon = tbSalon.Text;

            PersoaneContact pc = new PersoaneContact();
            pc.IdPacient = tbCNP.Text;
            pc.Nume = tbContactNume.Text;
            pc.Domiciliu = tbDomiciliu.Text;
            pc.Telefon = tbContactTelefon.Text;

            PersoanaTrimitere pt = new PersoanaTrimitere();
            pt.IdPacient = tbCNP.Text;
            pt.Nume = tbDoctorNume.Text;
            pt.Telefon = tbDoctorTelefon.Text;

            // Validare DateGeneralePacient
            toValidate.Add(tbCNP);
            toValidate.Add(tbPacientNume);
            toValidate.Add(tbPacientPrenume);
            toValidate.Add(cbSex);
            toValidate.Add(dtDataNasterii);
            toValidate.Add(tbLoculNasterii);
            toValidate.Add(tbResedinta);
            toValidate.Add(tbDomiciliuLegal);
            toValidate.Add(tbGrupaSange);
            toValidate.Add(tbOcupatie);
            toValidate.Add(tbLocMunca);
            toValidate.Add(tbTalonPensie);

            // Validare FiseInternare
            toValidate.Add(tbDiagnostic);
            toValidate.Add(tbDiagnostic72);
            toValidate.Add(tbDiagnosticTrimitere);
            toValidate.Add(dtDataInternarii);
            toValidate.Add(tbMotiveleInternarii);
            toValidate.Add(tbDiagnosticPrincipal);
            toValidate.Add(tbBoliConcomitente);           
            toValidate.Add(tbSectie);
            toValidate.Add(tbSalon);

            // Validare PersoaneContact
            toValidate.Add(tbContactNume);
            toValidate.Add(tbDomiciliu);
            toValidate.Add(tbContactTelefon);

            // Validare PersoanaTrimitere
            toValidate.Add(tbDoctorNume);
            toValidate.Add(tbDoctorTelefon);

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            Dictionary<string, string> errors = xmlParser.ExtractAttributePairs("MesajeEroare", string.Empty, string.Empty, "name", "formula");


            foreach (Control tb in toValidate)
            {
                Validation val = new Validation(tb, tb.Text);
                isValid = val.validate();

                if (isValid == false)
                {
                    string eroare = val.getError();
                    MessageBox.Show(errors["CampIncorect"], "Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    redControl = tb;
                    break;
                }
            }

            //salvare in BD            
            if (isValid == true)
            {
                try
                {

                    PersistenceManager pm = PersistenceManager.Instance;
                    pm.Save(dgp);
                    pm.Save(fi);
                    pm.Save(pc);
                    pm.Save(pt);
                    MessageBox.Show(errors["okBD"], "Information",
                           MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show(errors["ErrorBD"], "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

        }

        private void changeColor(Control cont)
        {
            if (cont.GetType() == typeof(TextBox))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
            if (cont.GetType() == typeof(DateTimePicker))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
            if (cont.GetType() == typeof(ComboBox))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
        }
    }
}
